
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import javax.swing.Timer;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Hra.java
 *
 * Created on Oct 16, 2011, 3:25:56 PM
 */
/**
 *
 * @author Riso-Desktop
 */
public class Hra extends javax.swing.JPanel implements MouseListener, Runnable, ActionListener {

    Timer t = new Timer(1000, this);
    /** Creates new form Hra */
    Thread hlavne = new Thread(this);
    sliepka_leti s;
    sliepka_leti_P sp;
    sliepka_kuk skuk;
    sliepka_leti_mala_P smp;
    sliepka_leti_mala_L sml;
    int nahoda;
    int nahodanenastala;
    boolean hrabezi = false;
    static int strely = 7;
    static int skore = 0;
    static int cas;

    public Hra() {
        addMouseListener(this);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();

        setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
        setOpaque(false);
        setLayout(null);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ammo.png"))); // NOI18N
        add(jLabel2);
        jLabel2.setBounds(930, 510, 12, 79);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ammo.png"))); // NOI18N
        add(jLabel3);
        jLabel3.setBounds(910, 510, 12, 79);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ammo.png"))); // NOI18N
        add(jLabel4);
        jLabel4.setBounds(890, 510, 12, 79);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ammo.png"))); // NOI18N
        add(jLabel5);
        jLabel5.setBounds(870, 510, 12, 79);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ammo.png"))); // NOI18N
        add(jLabel6);
        jLabel6.setBounds(850, 510, 12, 79);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ammo.png"))); // NOI18N
        add(jLabel7);
        jLabel7.setBounds(830, 510, 12, 79);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Score:");
        add(jLabel1);
        jLabel1.setBounds(330, 560, 80, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel8.setText("Time:");
        add(jLabel8);
        jLabel8.setBounds(10, 560, 70, 30);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("2");
        add(jLabel9);
        jLabel9.setBounds(90, 560, 30, 30);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel10.setText(":");
        add(jLabel10);
        jLabel10.setBounds(120, 560, 10, 30);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel11.setText("00");
        add(jLabel11);
        jLabel11.setBounds(130, 560, 40, 30);

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("0");
        add(jLabel12);
        jLabel12.setBounds(420, 560, 130, 30);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables

    public void novahra() {
        if (hrabezi != true) {
            hlavne = new Thread(this);
            hrabezi = true;
            strely = 7;
            skore = 0;
            cas = 120;
            hlavne.start();
            podmaz();
        }
    }
    AudioPlayer ap = AudioPlayer.player;
    AudioStream as;
    AudioData ad;
    AudioDataStream zvuk = null;
    AudioDataStream podmaz = null;

    private void vystrel() {
        try {
            as = new AudioStream(new FileInputStream("zvuky/Vystrel.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ap.start(zvuk);

    }

    private void podmaz() {
        try {
            as = new AudioStream(new FileInputStream("zvuky/chick1.wav"));
            ad = as.getData();
            podmaz = new AudioDataStream(ad);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ap.start(podmaz);
    }
    public void zvukstop(){
        ap.stop(podmaz);
    }

    private void nabitzvuk() {
        try {
            as = new AudioStream(new FileInputStream("zvuky/reload.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ap.start(zvuk);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }
    static boolean nabi = false;

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == e.BUTTON1) {

            switch (strely) {
                case 7:
                    vystrel();
                    skore -= 20;
                    strely--;
                    break;
                case 6:
                    vystrel();
                    skore -= 20;
                    strely--;
                    break;
                case 5:
                    vystrel();
                    skore -= 20;
                    strely--;
                    break;
                case 4:
                    vystrel();
                    skore -= 20;
                    strely--;
                    break;
                case 3:
                    vystrel();
                    skore -= 20;
                    strely--;
                    break;
                case 2:
                    vystrel();
                    skore -= 20;
                    strely--;
                    break;
                case 1:
                    strely--;
                    break;
            }
        }
        if (e.getButton() == e.BUTTON3 && strely <= 1) {
            nabi = true;

        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    public void nabi() {
        nabi = true;
    }

    @Override
    public void run() {

        t.start();
        while (hrabezi == true) {
            jLabel9.setText("" + cas / 60);
            if (cas % 60 < 10) {
                jLabel11.setText("0" + cas % 60);
            } else {
                jLabel11.setText("" + cas % 60);
            }

            jLabel12.setText("" + skore);

            if (nahodanenastala > 20) {
                nahoda = (int) (0 + Math.random() * 10);
            } else {
                nahoda = (int) (0 + Math.random() * 100);
            }

            if (nahoda == 1) {
                skuk = new sliepka_kuk(this);
                this.add(skuk);
                nahodanenastala = 0;
            } else if (nahoda < 6) {
                if (nahoda < 5) {
                    smp = new sliepka_leti_mala_P(this);
                    this.add(smp);
                    nahodanenastala = 0;
                } else {
                    sml = new sliepka_leti_mala_L(this);
                    this.add(sml);
                    nahodanenastala = 0;
                }
            } else if (nahoda <= 9) {
                if (nahoda < 7) {
                    s = new sliepka_leti(this);
                    this.add(s);
                    nahodanenastala = 0;
                } else {
                    sp = new sliepka_leti_P(this);
                    this.add(sp);
                    nahodanenastala = 0;
                }
            } else {
                nahodanenastala++;
            }
            try {
                hlavne.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
            switch (strely) {
                case 6:
                    jLabel7.setVisible(false);
                    break;
                case 5:
                    jLabel6.setVisible(false);
                    break;
                case 4:
                    jLabel5.setVisible(false);
                    break;
                case 3:
                    jLabel4.setVisible(false);
                    break;
                case 2:
                    jLabel3.setVisible(false);
                    break;
                case 1:
                    jLabel2.setVisible(false);
                    break;
            }
            if (strely <= 1 && nabi == true) {
                strely = 7;
                nabitzvuk();
                jLabel2.setVisible(true);
                jLabel3.setVisible(true);
                jLabel4.setVisible(true);
                jLabel5.setVisible(true);
                jLabel6.setVisible(true);
                jLabel7.setVisible(true);
                nabi = false;
            }

        }
        t.stop();



    }

    @Override
    public void actionPerformed(ActionEvent e) {
        cas--;
        if (cas == 0) {
            t.stop();
            hrabezi = false;
        }
    }
}
